package com.louis.buddhist.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.louis.buddhist.R;
import com.louis.buddhist.data.entity.BookList;
import com.louis.buddhist.widget.view.DragGridListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2015/12/17.
 */
public class ShelfAdapter extends BaseAdapter implements DragGridListener{
    private Context mContex;
    private List<BookList.EbookListBean> bilist;
    private static LayoutInflater inflater = null;
    private int mHidePosition = -1;

    public ShelfAdapter(Context context, List<BookList.EbookListBean> bilist){
        this.mContex = context;
        this.bilist = bilist;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        //背景书架的draw需要用到item的高度
        if(bilist.size() < 1){
            return 1;
        }else{
            return bilist.size();
        }
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return bilist.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View contentView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        final ViewHolder viewHolder;
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.shelfitem, null);
            viewHolder = new ViewHolder(contentView);
            contentView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) contentView.getTag();
        }

        if(bilist.size() > position){
            //DragGridView  解决复用问题
            if(position == mHidePosition){
                contentView.setVisibility(View.INVISIBLE);
            }else {
                contentView.setVisibility(View.VISIBLE);
            }
            viewHolder.name.setVisibility(View.VISIBLE);
            String fileName = bilist.get(position).getTitle();
            String url = bilist.get(position).getCover();
            viewHolder.name.setText(fileName);
            Glide.with(mContex).load(url).into(viewHolder.pic);

        }else {
            contentView.setVisibility(View.INVISIBLE);
        }
        return contentView;
    }

    @Override
    public void reorderItems(int oldPosition, int newPosition) {

    }

    @Override
    public void setHideItem(int hidePosition) {

    }

    @Override
    public void removeItem(int deletePosition) {

    }

    @Override
    public void setItemToFirst(int openPosition) {

    }

    @Override
    public void nitifyDataRefresh() {

    }

    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView name;
        @BindView(R.id.iv_pic)
        ImageView pic;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void setBookList(List<BookList.EbookListBean> bookLists){
        this.bilist = bookLists;
        notifyDataSetChanged();
    }

}
